package com.qidian.mes.excel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * @Author 戴着假发的程序员
 * @Company 起点编程
 * @Description
 * @Time 2024/4/12 9:36
 */
// 我们自定义的转换器要实现接口com.alibaba.excel.converters.Converter
// 这里的泛型是java对象的属性的数据类型
public class UserGenderConverter implements Converter<Integer> {
    /**
     * 将Java Data 转换为 Excel Data   将整型数字转换为字符串
     * @param value               Java Data.NotNull.
     * @param contentProperty     Content property.Nullable.
     * @param globalConfiguration Global configuration.NotNull.
     * @return
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new WriteCellData<>(value == 1 ? "男" : value == 2 ? "女" : "未知");
    }

    /**
     * 将字符串转换为整型
     * @param context read converter context
     * @return
     * @throws Exception
     */
    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) throws Exception {
        String gender = context.getReadCellData().getStringValue();
        return gender.equals("男") ? 1 : gender.equals("女") ? 2 : 3;
    }
}
